package faw.car.ccenter.utils;

import android.util.Log;

import java.util.Locale;

public class FLog {
    private static final String PREFIX = "CCenter_";
    private static final String TAG = PREFIX + "FLog";
    private static boolean mDebugging = false;
    private static LogLevel mLogLevel = LogLevel.I;

    //private static final Gson sGson;

    private FLog() {
    }

    public static synchronized void setLogLevel(LogLevel level) {
        if (mLogLevel == level)
            return;
        Log.i(TAG, "setLogLevel -> [" + level + "]");
        mLogLevel = level;
    }

    public static synchronized void setDebug(boolean debug) {
        mDebugging = debug;
    }

    public static synchronized boolean isDebug() {
        return mDebugging;
    }

    public static void v(String tag, String message) {
        if (isDebug() || LogLevel.V.compareTo(mLogLevel) >= 0) {
            Log.v(PREFIX + tag, message);
        }
    }

    public static void vPrivate(String tag, String message, String... privates) {
        if (isDebug() && LogLevel.V.compareTo(mLogLevel) >= 0) {
            Log.v(PREFIX + tag, message);
        } else if (LogLevel.V.compareTo(mLogLevel) >= 0) {
            String[] var3 = privates;
            int var4 = privates.length;
            for (int var5 = 0; var5 < var4; ++var5) {
                String aPrivate = var3[var5];
                message = message.replace(aPrivate, "<private>");
            }
            Log.v(PREFIX + tag, message);
        }
    }

    public static void d(String tag, String message) {
        if (isDebug() || LogLevel.D.compareTo(mLogLevel) >= 0) {
            Log.d(PREFIX + tag, message);
        }
    }

    public static void dPrivate(String tag, String message, String... privates) {
        if (isDebug() && LogLevel.D.compareTo(mLogLevel) >= 0) {
            Log.d(PREFIX + tag, message);
        } else if (LogLevel.D.compareTo(mLogLevel) >= 0) {
            String[] var3 = privates;
            int var4 = privates.length;
            for (int var5 = 0; var5 < var4; ++var5) {
                String aPrivate = var3[var5];
                message = message.replace(aPrivate, "<private>");
            }
            Log.d(PREFIX + tag, message);
        }
    }

    public static void i(String tag, String message) {
        if (isDebug() || LogLevel.I.compareTo(mLogLevel) >= 0) {
            Log.i(PREFIX + tag, message);
        }
    }

    public static void iPrivate(String tag, String message, String... privates) {
        if (isDebug() && LogLevel.I.compareTo(mLogLevel) >= 0) {
            Log.i(PREFIX + tag, message);
        } else if (LogLevel.I.compareTo(mLogLevel) >= 0) {
            String[] var3 = privates;
            int var4 = privates.length;

            for (int var5 = 0; var5 < var4; ++var5) {
                String aPrivate = var3[var5];
                message = message.replace(aPrivate, "<private>");
            }
            Log.i(PREFIX + tag, message);
        }
    }

    public static void w(String tag, String message) {
        if (isDebug() || LogLevel.W.compareTo(mLogLevel) >= 0) {
            Log.w(PREFIX + tag, message);
        }
    }

    public static void wPrivate(String tag, String message, String... privates) {
        if (isDebug() && LogLevel.W.compareTo(mLogLevel) >= 0) {
            Log.w(PREFIX + tag, message);
        } else if (LogLevel.W.compareTo(mLogLevel) >= 0) {
            String[] var3 = privates;
            int var4 = privates.length;
            for (int var5 = 0; var5 < var4; ++var5) {
                String aPrivate = var3[var5];
                message = message.replace(aPrivate, "<private>");
            }
            Log.w(PREFIX + tag, message);
        }
    }

    public static void e(String tag, String message) {
        Log.e(PREFIX + tag, message);
    }

    public static void ePrivate(String tag, String message, String... privates) {
        if (isDebug() && LogLevel.E.compareTo(mLogLevel) >= 0) {
            Log.e(PREFIX + tag, message);
        } else if (LogLevel.E.compareTo(mLogLevel) >= 0) {
            String[] var3 = privates;
            int var4 = privates.length;
            for (int var5 = 0; var5 < var4; ++var5) {
                String aPrivate = var3[var5];
                message = message.replace(aPrivate, "<private>");
            }
            Log.e(PREFIX + tag, message);
        }
    }

    public static void wtf(String tag, String message) {
        Log.wtf(PREFIX + tag, message);
    }

    public static String generatePrefix(String message, String appName, String versionName) {
        return format(Locale.getDefault(), "(%s:%s)%s", appName, versionName, message);
    }

    public static String format(Locale l, String format, Object... args) {
        try {
            return String.format(l, format, args);
        } catch (Throwable var4) {
            return format;
        }
    }

    /**
     * 由于fastddslib.jar包中需要额外依赖Gson，Json格式化打印暂时弃用。
     */
//    public static void prettyPrinting(String tag, Object src) {
//        if (src == null) return;
//        FLog.i(PREFIX + tag, sGson.toJson(src));
//    }
//    public static String prettyString(Object src) {
//        try {
//            return unsafePrettyString(src);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "";
//    }
//    public static String unsafePrettyString(Object src) throws JsonIOException {
//        return sGson.toJson(src);
//    }
//    static {
//        mLogLevel = FLog.LogLevel.D;
//        sGson = new GsonBuilder()
//                .serializeNulls()
//                //.setPrettyPrinting() //格式化输出结构体！
//                .create();
//    }
    public static enum LogLevel {
        V,
        D,
        I,
        W,
        E;

        private LogLevel() {
        }
    }
}
